package server;

import client.MapleCharacter;
import handling.channel.ChannelServer;
import server.maps.MapleMap;
import tools.MaplePacketCreator;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MapleCarnivalParty {
    private List<Integer> members;
    private WeakReference<MapleCharacter> leader;
    private byte team;
    private int channel;
    private short availableCP;
    private short totalCP;
    private boolean winner;
    private ReentrantReadWriteLock memebersLock;

    public MapleCarnivalParty(MapleCharacter owner, List<MapleCharacter> members, byte team1) {
        this.members = (List<Integer>) new LinkedList();
        this.availableCP = 0;
        this.totalCP = 0;
        this.winner = false;
        this.memebersLock = new ReentrantReadWriteLock();
        this.leader = new WeakReference(owner);
        for (MapleCharacter mem : members) {
            this.addMember(mem.getId());
        }
        this.team = team1;
        this.channel = owner.getClient().getChannel();
    }

    public MapleCharacter getLeader() {
        return (MapleCharacter) this.leader.get();
    }

    public void addCP(MapleCharacter player, int ammount) {
        this.totalCP += (short) ammount;
        this.availableCP += (short) ammount;
        player.addCP(ammount);
    }

    public int getTotalCP() {
        return this.totalCP;
    }

    public int getAvailableCP() {
        return this.availableCP;
    }

    public void useCP(MapleCharacter player, int ammount) {
        if (this.availableCP >= ammount) {
            this.availableCP -= (short) ammount;
        } else {
            this.availableCP = 0;
        }
        player.useCP(ammount);
    }

    public List<Integer> getMembers() {
        return this.members;
    }

    public int getTeam() {
        return this.team;
    }

    public void warp(MapleMap map, String portalname) {
        this.memebersLock.readLock().lock();
        try {
            Iterator<Integer> iterator = this.members.iterator();
            while (iterator.hasNext()) {
                int chr = ((Integer) iterator.next()).intValue();
                MapleCharacter c = ChannelServer.getInstance(this.channel).getPlayerStorage().getCharacterById(chr);
                if (c != null) {
                    c.changeMap(map, map.getPortal(portalname));
                }
            }
        }
        finally {
            this.memebersLock.readLock().unlock();
        }
    }

    public void warp(MapleMap map, int portalid) {
        this.memebersLock.readLock().lock();
        try {
            Iterator<Integer> iterator = this.members.iterator();
            while (iterator.hasNext()) {
                int chr = ((Integer) iterator.next()).intValue();
                MapleCharacter c = ChannelServer.getInstance(this.channel).getPlayerStorage().getCharacterById(chr);
                if (c != null) {
                    c.changeMap(map, map.getPortal(portalid));
                }
            }
        }
        finally {
            this.memebersLock.readLock().unlock();
        }
    }

    public boolean allInMap(MapleMap map) {
        try {
            this.memebersLock.readLock().lock();
            Iterator<Integer> iterator = this.members.iterator();
            while (iterator.hasNext()) {
                int chr = ((Integer) iterator.next()).intValue();
                if (map.getCharacterById(chr) == null) {
                    return false;
                }
            }
        }
        finally {
            this.memebersLock.readLock().unlock();
        }
        return true;
    }

    public void removeMember(MapleCharacter chr) {
        try {
            this.memebersLock.writeLock().lock();
            int index = -1;
            for (int i = 0; i < this.members.size(); ++i) {
                if (((Integer) this.members.get(i)).intValue() == chr.getId()) {
                    index = i;
                }
            }
            if (index != -1) {
                chr.setCarnivalParty(null);
                this.members.remove(index);
            }
        }
        finally {
            this.memebersLock.writeLock().unlock();
        }
    }

    public boolean isWinner() {
        return this.winner;
    }

    public void setWinner(boolean status) {
        this.winner = status;
    }

    public void displayMatchResult() {
        String effect = this.winner ? "quest/carnival/win" : "quest/carnival/lose";
        String sound = this.winner ? "MobCarnival/Win" : "MobCarnival/Lose";
        boolean done = false;
        Iterator<Integer> iterator = this.members.iterator();
        while (iterator.hasNext()) {
            int chr = ((Integer) iterator.next()).intValue();
            MapleCharacter c = ChannelServer.getInstance(this.channel).getPlayerStorage().getCharacterById(chr);
            if (c != null) {
                c.getClient().sendPacket(MaplePacketCreator.showEffect(effect));
                c.getClient().sendPacket(MaplePacketCreator.playSound(sound));
                if (done) {
                    continue;
                }
                done = true;
                c.getMap().killAllMonsters(true);
                c.getMap().setSpawns(false);
            }
        }
    }

    private void addMember(int charId) {
        try {
            this.memebersLock.writeLock().lock();
            this.members.add(Integer.valueOf(charId));
        }
        finally {
            this.memebersLock.writeLock().unlock();
        }
    }
}
